{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Padovan sequence\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Ex9.%20Padovan%20(Solved).ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FEx9.%20Padovan%20(Solved).ipynb)\n", "\n", "The Padovan sequence is the following infinite sequence of natural numbers:\n", "`1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 ...`\n", "\n", "The sequence begins with the subsquence $[1, 1, 1]$ and then each term is the sum of two terms before the last one, that is:\n", "\n", "$P(0) = 1$\n", "\n", "$P(1) = 1$\n", "\n", "$P(2) = 1$\n", "\n", "$P(n) = P(n-2) + P(n-3)$\n", "\n", "The Padovan sequence is a cousin of the Fibonacci sequence and has many applications in applied mathematics. As a curiosity, the Padovan sequence can be represented as a set of joined equilateral triangles forming an spiral:\n", "\n", "\"Padovan\n", "\n", "Recall that in Python, the last elements of the array can be accessed directly using negative indices, for example, `array[-1]` would be the last element of the array, `array [-2]` the second to last, and so on. Also, remember that the `append()` function, adds an element to the end of the array.\n", "\n", "With everything explained so far, **develop the following program**:\n", "\n", "A program that asks the user for a length and generates a Padovan sequence of that length, stores it in an array and prints the array. " ] }, { "cell_type": "markdown", "source": [ "# Solution\n", "## Flow chart\n", "The following flow chart represents a possible solution to the problem:\n", "\n", "![Padovan Sequence](img/Padovan.png)\n", "\n", "We can generate a Padovan sequence with 4 elements as:\n", "\n", "\n", "```python\n", "pad = [1, 1, 1]\n", "next_val = pad[-2] + pad[-3]\n", "pad.append(next_val)\n", "```\n", "\n", "Note that the value that we have added does not depend on the length of the array, but on the last two elements of the array. Therefore, we can use a loop to generate the sequence of any length.\n", "\n", "Also, in Python, we can use the `while` loop to iterate while a certain condition is not met. We can use this type of loop to generate a Padovan sequence with a given length, using the ```len``` function in the condition to compare the actual length of the array with the value provided by the user.\n", "\n", "## Code\n", "This is the code of the solution:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "# Get length of sequence to generate from input\n", "padovan_len = int(input(\"enter length of sequence: \"))\n", "# Init Padovan sequence array\n", "pad = [1, 1, 1]\n", "\n", "# While the length of the sequence is smaller than the input\n", "while len(pad) < padovan_len:\n", " # Calculate the next value as the sum of the two values before the last one\n", " next_val = pad[-2] + pad[-3]\n", " # Append the new value to the array\n", " pad.append(next_val)\n", "\n", "print(pad)\n" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 4 }